<script setup>
import service from '@/utils/request.utils';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
import { useStore } from 'vuex'
import 'highlight.js/styles/monokai.css';
import Markdown from 'vue3-markdown-it';

const store = useStore()

const history = ref([
    {
        role: "AI助手",
        message: "请问我有什么可以帮到你的吗？"
    },
    // },
    // {
    //     role: "人类",
    //     message: "请问 1 + 1 等于多少？"
    // },
    // {
    //     role: "AI助手",
    //     message: "1+1的结果为2"
    // }
])
const question = ref(null)
const btn = ref(false)

async function send() {

    btn.value = true
    if (question.value == null || question.value.lenght == 0) {
        ElMessage.warning("聊天输入不能为空")
        return
    }

    const message = question.value
    question.value = null


    try {
        let llm = store.state.model
        let temperature = store.state.temperature
        let res = service.post("/chat/putty", { "history": history.value, "question": message, "llm": llm, "temperature": temperature })
        history.value.push({ "role": "人类", "message": message })
        res = await res;
        console.log(res)

        if (res.status != 200) {
            ElMessage({
                message: '操作失败: ' + res.status,
                type: 'error'
            })
        }

        if (res.data.code != '0') {
            console.log(res.data)
            ElMessage({
                message: RESPONSE_MESSAGE[res.data.code],
                type: "warning"
            })
        } else {
            let data = res.data.data

            if (data.indexOf("AI助手：") == 0 || data.indexOf("AI助手:") == 0) {
                data = data.slice(5)
            }

            history.value.push({ role: "AI助手", message: data })
            btn.value = false
            return
        }

        history.value.pop()
        btn.value = false
        return
    } catch (e) {
        console.log(e)
        ElMessage.error(e)
        history.value.pop()
        btn.value = false
        return
    }
}


</script>

<template>
    <div id="outer">
        <h2 style="position: absolute;top: -15px">Chat</h2>
        <div id="chat-framework">
            <div id="chat-content">
                <div class="chat-message-item" v-for="item in  history ">
                    <div class="chat-human-message" v-if="item.role == '人类'">
                        <div class="human-content content-padding">
                            {{ item.message }}
                        </div>
                        <div class="user">
                            你
                        </div>
                    </div>
                    <div class="chat-ai-message" v-else>
                        <div class="user">
                            AI
                        </div>
                        <div class="ai-content content-padding">
                            <Markdown class="markdown-style" :source="item.message"></Markdown>
                        </div>
                    </div>
                </div>
            </div>
            <div id="chat-input">
                <el-input placeholder="请输入聊天信息" v-model="question" @keyup.enter.native="send"
                    id="chat-input-box"></el-input>
                <el-button id="chat-input-btn" @click="send" :disabled="btn" type="success">发送</el-button>
            </div>
        </div>
    </div>
</template>

<style scoped>
#outer {
    position: relative;
    margin: auto;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#chat-framework {
    width: 90%;
    height: 90%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
}

#chat-content {
    height: 90%;
    width: 100%;
    overflow: auto;
    border: 2px rgb(120, 120, 120) dashed;
    border-radius: 5px;
    background-color: rgb(245, 245, 245);
}

.chat-message-item {
    height: fit-content;
    width: 100%;
    position: relative;
    margin-top: 5px;
    margin-bottom: 5px;
}

.chat-human-message {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    width: 100%;
    height: fit-content;
}

.chat-ai-message {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    width: 100%;
    height: fit-content;
}

.content-padding {
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 7px;
    padding-bottom: 7px;
}

.human-content {
    max-width: 50%;
    background-color: goldenrod;
    border-radius: 5px;
}

.ai-content {
    width: fit-content;
    max-width: 50%;
    background-color: white;
    border-radius: 5px;
}

.user {
    margin-left: 0.3em;
    margin-right: 0.3em;
    margin-top: 5px;
    font-size: 1.2em;
    width: fit-content;
    font-weight: 700;
}


#chat-input {
    height: 10%;
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}
</style>


<style lang="less">
.markdown-style {
    margin-top: -15px;
    margin-bottom: -15px;
}
</style>